package com.zxq.web.assist;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxq.web.controller.IOTPoemController;
import com.zxq.web.dto.PlatformProductDTO;
import com.zxq.web.entity.IotProductDo;
import com.zxq.web.enums.EProductState;
import com.zxq.web.service.impl.IotProductDoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
@Slf4j
public class ActivateServer {



    @Autowired
    IotProductDoServiceImpl iotProductDoService;

    @Autowired
    IOTPoemController iotPoemController;

    /**
     * 重启产品任务
     */
    @PostConstruct
    public void restartProduct() {

        IotProductDo queryCondition = new IotProductDo();
        queryCondition.setState(EProductState.run.getValue());
        QueryWrapper<IotProductDo> queryWrapper = new QueryWrapper<>(queryCondition);
        List<IotProductDo> productDoList =  iotProductDoService.list(queryWrapper);
        if(productDoList.size() == 0) {
            log.info("not restart product!");
            return;
        }
        productDoList.forEach(iotProductDo -> {
            PlatformProductDTO productDTO = new PlatformProductDTO();
            productDTO.setProductId(iotProductDo.getId());
            productDTO.setOperatorId(iotProductDo.getCreator());
            iotPoemController.startProductServer(productDTO);
        });

    }



}
